package de.jmda.home.ui.vaadin.userguide;

import java.io.File;

import com.google.common.eventbus.Subscribe;
import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.BrowserFrame;
import com.vaadin.ui.VerticalLayout;

import de.jmda.common.ui.web.vaadin.service.DefaultSessionContext;

public class UserGuideDisplay
{
	public enum UserGuideChapter
	{
		HOME,
		INTRODUCTION,
				MODELS,
				LIBRARIES_IN_INTRODUCTION,
		GETTING_STARTED,
				READ_MODELS,
				GENERATE_CODE,
				PROCESS_VISUALLY,
		UNDER_THE_HOOD,
				WHY_NOT,
		ROADMAP,
				JPA,
		LIBRARIES,
				CORE,
				GENERATE,
				CORE_SWING_UI,
		DAY_TO_DAY_JMDA,
		OTHER_APPROACHES
		;
		
		public final static UserGuideChapter FIRST;

		static
		{
			HOME.prev = null;
			HOME.next = INTRODUCTION;
			HOME.parent = null;
			HOME.children = new UserGuideChapter[] {};
			HOME.caption = "home";
			HOME.path = "/resource/userguide/mission.statement_why.jmda.xhtml";
			INTRODUCTION.prev = HOME;
			INTRODUCTION.next = GETTING_STARTED;
			INTRODUCTION.parent = null;
			INTRODUCTION.children = new UserGuideChapter[] { MODELS, LIBRARIES_IN_INTRODUCTION};
			INTRODUCTION.caption = "introduction";
			INTRODUCTION.path = "/resource/userguide/introduction.xhtml";
					MODELS.prev = INTRODUCTION;
					MODELS.next = LIBRARIES_IN_INTRODUCTION;
					MODELS.parent = INTRODUCTION;
					MODELS.children = new UserGuideChapter[] {};
					MODELS.caption = "models";
					MODELS.path = "/resource/userguide/introduction.models.xhtml";
					LIBRARIES_IN_INTRODUCTION.prev = MODELS;
					LIBRARIES_IN_INTRODUCTION.next = GETTING_STARTED;
					LIBRARIES_IN_INTRODUCTION.parent = INTRODUCTION;
					LIBRARIES_IN_INTRODUCTION.children = new UserGuideChapter[] {};
					LIBRARIES_IN_INTRODUCTION.caption = "libraries";
					LIBRARIES_IN_INTRODUCTION.path = "/resource/userguide/introduction.libraries.xhtml";
			GETTING_STARTED.prev = INTRODUCTION;
			GETTING_STARTED.next = UNDER_THE_HOOD;
			GETTING_STARTED.parent = null;
			GETTING_STARTED.children = new UserGuideChapter[] { READ_MODELS, GENERATE_CODE, PROCESS_VISUALLY };
			GETTING_STARTED.caption = "getting started";
			GETTING_STARTED.path = "/resource/userguide/gettingStarted.xhtml";
					READ_MODELS.prev = GETTING_STARTED;
					READ_MODELS.next = GENERATE_CODE;
					READ_MODELS.parent = GETTING_STARTED;
					READ_MODELS.children = new UserGuideChapter[] {};
					READ_MODELS.caption = "read models";
					READ_MODELS.path = "/resource/userguide/gettingStarted.readModels.xhtml";
					GENERATE_CODE.prev = READ_MODELS;
					GENERATE_CODE.next = PROCESS_VISUALLY;
					GENERATE_CODE.parent = GETTING_STARTED;
					GENERATE_CODE.children = new UserGuideChapter[] {};
					GENERATE_CODE.caption = "generate code";
					GENERATE_CODE.path = "/resource/userguide/gettingStarted.generatorCode.xhtml";
					PROCESS_VISUALLY.prev = GENERATE_CODE;
					PROCESS_VISUALLY.next = UNDER_THE_HOOD;
					PROCESS_VISUALLY.parent = GETTING_STARTED;
					PROCESS_VISUALLY.children = new UserGuideChapter[] {};
					PROCESS_VISUALLY.caption = "process visually";
					PROCESS_VISUALLY.path = "/resource/userguide/gettingStarted.visualProcessor.xhtml";
			UNDER_THE_HOOD.prev = GETTING_STARTED;
			UNDER_THE_HOOD.next = WHY_NOT;
			UNDER_THE_HOOD.parent = null;
			UNDER_THE_HOOD.children = new UserGuideChapter[] { WHY_NOT };
			UNDER_THE_HOOD.caption = "under the hood";
			UNDER_THE_HOOD.path = "/resource/userguide/underTheHood.xhtml";
					WHY_NOT.prev = UNDER_THE_HOOD;
					WHY_NOT.next = ROADMAP;
					WHY_NOT.parent = UNDER_THE_HOOD;
					WHY_NOT.children = new UserGuideChapter[] {};
					WHY_NOT.caption = "why not ...";
					WHY_NOT.path = "/resource/userguide/underTheHood.whyNot.xhtml";
			ROADMAP.prev = UNDER_THE_HOOD;
			ROADMAP.next = JPA;
			ROADMAP.parent = null;
			ROADMAP.children = new UserGuideChapter[] { JPA };
			ROADMAP.caption = "roadmap";
			ROADMAP.path = "/resource/userguide/project.roadmap.xhtml";
					JPA.prev = ROADMAP;
					JPA.next = LIBRARIES;
					JPA.parent = ROADMAP;
					JPA.children = new UserGuideChapter[] {};
					JPA.caption = "JPA";
					JPA.path = "/resource/userguide/project.roadmap.jpa.xhtml";
			LIBRARIES.prev = ROADMAP;
			LIBRARIES.next = CORE;
			LIBRARIES.parent = null;
			LIBRARIES.children = new UserGuideChapter[] { CORE, GENERATE, CORE_SWING_UI };
			LIBRARIES.caption = "libraries";
			LIBRARIES.path = "/resource/userguide/libraries.xhtml";
					CORE.prev = LIBRARIES;
					CORE.next = GENERATE;
					CORE.parent = LIBRARIES;
					CORE.children = new UserGuideChapter[] {};
					CORE.caption = "libraries";
					CORE.path = "/resource/userguide/librariesCore.xhtml";
					GENERATE.prev = CORE;
					GENERATE.next = CORE_SWING_UI;
					GENERATE.parent = LIBRARIES;
					GENERATE.children = new UserGuideChapter[] {};
					GENERATE.caption = "generate";
					GENERATE.path = "/resource/userguide/librariesGenerate.xhtml";
					CORE_SWING_UI.prev = GENERATE;
					CORE_SWING_UI.next = DAY_TO_DAY_JMDA;
					CORE_SWING_UI.parent = LIBRARIES;
					CORE_SWING_UI.children = new UserGuideChapter[] {};
					CORE_SWING_UI.caption = "core swing ui";
					CORE_SWING_UI.path = "/resource/userguide/librariesCoreSwingUI.xhtml";
			DAY_TO_DAY_JMDA.prev = LIBRARIES;
			DAY_TO_DAY_JMDA.next = OTHER_APPROACHES;
			DAY_TO_DAY_JMDA.parent = null;
			DAY_TO_DAY_JMDA.children = new UserGuideChapter[] {};
			DAY_TO_DAY_JMDA.caption = "day-to-day jMDA";
			DAY_TO_DAY_JMDA.path = "/resource/userguide/day-to-day-jMDA.xhtml";
			OTHER_APPROACHES.prev = DAY_TO_DAY_JMDA;
			OTHER_APPROACHES.next = null;
			OTHER_APPROACHES.parent = null;
			OTHER_APPROACHES.children = new UserGuideChapter[] {};
			OTHER_APPROACHES.caption = "other MDA approaches";
			OTHER_APPROACHES.path = "/resource/userguide/otherMDAApproaches.xhtml";
					
			FIRST = HOME;
		}
		
		private UserGuideChapter prev;
		private UserGuideChapter next;
		private UserGuideChapter parent;
		private UserGuideChapter[] children;
		private String caption;
		private String path;

		public UserGuideChapter getPrev()
		{
			return prev;
		}
		public UserGuideChapter getNext()
		{
			return next;
		}
		public UserGuideChapter getParent()
		{
			return parent;
		}
		public UserGuideChapter[] getChildren()
		{
			return children;
		}
		public String getCaption()
		{
			return caption;
		}
		public String getPath()
		{
			return path;
		}

		@Override
    public String toString()
    {
	    return getCaption();
    }
	}
	
	private VerticalLayout layout;

	public UserGuideDisplay(VerticalLayout layout)
	{
		this.layout = layout;
		
		layout.setSizeFull();
		layout.setMargin(true);
		
		DefaultSessionContext.get().getEventBus().register(this);
	}

	@Subscribe
	public void handle(UserGuideChapterDisplayRequest event)
	{
		String basepath =
				VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();

		String path = basepath + event.getUserGuideChapter().getPath();
		
		BrowserFrame browserFrame = new BrowserFrame();

		browserFrame.setSource(new FileResource(new File(path)));
		browserFrame.setAlternateText("failure displaying page for " + path);
		browserFrame.setSizeFull();

		layout.removeAllComponents();
		layout.addComponent(browserFrame);
		layout.setExpandRatio(browserFrame, 1.0f);
	}
}